package com.story.api.moduler.song.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.story.api.common.persistence.dao.PlayRecordMapper;
import com.story.api.common.persistence.model.PlayRecord;
import com.story.api.moduler.song.dao.VedioDetailDao;
import com.story.api.moduler.song.service.PlayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yangbo
 * @date 2019-09-23 17:18
 */
@Service
public class PlayRecordServiceImpl implements PlayRecordService {

    @Autowired
    private VedioDetailDao vedioDetailDao;

    @Autowired
    private PlayRecordMapper playRecordMapper;

    @Override
    @Async
    public void playRecord(Integer userId,String filePath) {
        Map<String,Object> resourceMap = vedioDetailDao.getIdAndResTypeByFilePath(filePath);
        if(!resourceMap.isEmpty()) {
            Integer resourceId = (Integer) resourceMap.get("id");
            Integer resType = (Integer) resourceMap.get("resType");

            //删除原有的播放记录
            PlayRecord delPlayRecord = new PlayRecord();
            delPlayRecord.setUserId(userId);
            delPlayRecord.setResourceId(resourceId);
            delPlayRecord.setResType(resType);
            playRecordMapper.delete(new EntityWrapper<>(delPlayRecord));

            //保存记录与用户的关联关系
            PlayRecord playRecord = new PlayRecord();
            playRecord.setUserId(userId);
            playRecord.setResourceId(resourceId);
            playRecord.setResType(resType);
            playRecord.setPlayTime(new Date());
            playRecordMapper.insert(playRecord);
        }
    }


}
